
#ifndef __IEC101PROTOCALDEFINEV1_H__
#define __IEC101PROTOCALDEFINEV1_H__

#include <stdint.h> 

#include <stdbool.h>

#define IEC101_FRAME_FIX_SIZE       sizeof(FIXEDFRAME)
#define IEC101_FRAME_VAR_HEAD_SIZE  sizeof(FRAME_VAR_HEAD)
#define REC_WAVE_CHANNEL_NUM        7
#define MAX_POINT_NUM_PER_PACK      11

#define DEBUG_IEC 1
#define TRUE 1
#define FALSE 0


//�̶�֡
#define RT_USING_TINY_SIZE 1

typedef enum
{
	enum_WiredNetwork = 0,
	enum_4G = 1,
	enum_SerialPort =2
} Comm_Type;



//�����ṹ��
typedef  struct{
	uint8_t Ia[3];
	uint8_t Ib[3];
	uint8_t Ic[3];
	uint8_t Va[3];
	uint8_t Vb[3];
	uint8_t Vc[3];
	uint8_t In[3];
    uint8_t RES[3];  
} ADCHANNELS_TypeDef;

typedef struct
{
	uint16_t currentSecond;
	uint16_t currentPosInSecond;
}POSofCURRDataBLOCK;


extern ADCHANNELS_TypeDef *ptrBlockStartPOs;
extern ADCHANNELS_TypeDef *ptrCurrPos;
extern POSofCURRDataBLOCK currPOs;

typedef struct  FRAME_FIX
{
	unsigned char    ucStartChar;
	unsigned char    ucLinkControl;
	uint16_t         u16LinkAddress;
	unsigned char    ucCheckCode;
	unsigned char    ucEndChar;
} FIXEDFRAME;


//�ɱ�֡��֡ͷ
typedef struct 
{
	unsigned char    ucStartChar1;
	unsigned char    ucLen1;
	unsigned char    ucLen2;
	unsigned char    ucStartChar2;
	unsigned char    ucLinkControl;
	uint16_t         u16LinkAddress;
	unsigned char    ucTI; //���ͱ�ʶ��
	unsigned char    ucVSQ;//�ɱ�֡���޶�
	uint16_t         u16COT;//����ԭ��
	uint16_t         u16ASDUAddr;//ASDU������ַ
} FRAME_VAR_HEAD;

typedef  struct
{
    unsigned char    ucStartChar1;
	unsigned char    ucLen1;
	unsigned char    ucLen2;
	unsigned char    ucStartChar2;
	unsigned char    ucLinkControl;
	uint16_t         u16LinkAddress;
} FrameHead_Type;


//¼������֡ͷ
typedef  struct 
{
	FRAME_VAR_HEAD FVH;
	uint16_t      u16InfoOBJAddress;
    //Ϊ��ͳһ¼������֡�ṹ��ȥ���������ֶ�
    /*unsigned char    ucOperationID;
	unsigned __int32 u32FileID;*/
	uint32_t      u32Res;//��Ϊ�����ֶ�
	unsigned char    ucEndFlag;
} RECORD_WAVE_HEAD;

extern void MakeFixFrame(FIXEDFRAME* fixFrame, unsigned char  _ucLinkControl,uint16_t  _u16LinkAddress);

//�������ܼ���֡У���
extern unsigned char CacuCheckCS(unsigned char* pucStart,int nSize);

typedef  struct time
{
	uint8_t sec;
	uint8_t min;
	uint8_t hour;
	uint8_t week;
	uint8_t day;
	uint8_t month;
	uint16_t year;

}TimeStruct;

typedef  struct 
{
	uint8_t millisec;
	uint8_t min;
	uint8_t hour;
	uint8_t week;
	uint8_t day;
	uint8_t month;
	uint16_t year;
}TimeStructMilliSec;

/*
--------------------------------------------------------------------
    |   7   |   6   |   5   |   4   |   3   |   2   |   1   |  0   |
--------------------------------------------------------------------
8   |     Milliseconds(D7---D0)                                    |
16  |     Milliseconds(D15---D8)                                   |
--------------------------------------------------------------------
24  | IV    |  RES1 |  Minutes(D5�D0)                              |
--------------------------------------------------------------------
32  | SU    |  RES2         |Hours(D4�D0)                          |
--------------------------------------------------------------------
40  |Day of Week(D7�D5)     |Day of Month(D4�D0)                   |
--------------------------------------------------------------------
48  |RES3(D7�D4)                    | Months(D3�D0)                |
--------------------------------------------------------------------
56  |RES4   |Years(D6�D0)                                          |

     Milliseconds = UI16[1..16]<0..59999> 
     Minutes      = UI6[17..22]<0..59   >  
	 RES1         = BS1[23    ]  
	 IV           = BS1[24    ]<0..1    > (IV<0> = ??;IV<1> = ???) IV = Invalid
	 Hours        = UI5[25..29]<0..23   >
	 RES2         = BS2[30..31] 
	 SU           = BS1[32    ]<0..1    > (SU<0> = ????;SU<1> = ????) SU = Summer
	 Day of Month = UI5[33..37]<1..31   >  
	 Day of Week  = UI3[38..40]<1..7    >
	 Months       = UI4[41..44]<1..12   >   
	 RES3         = BS4[45..48] 
	 Years        = BS4[49..55]<0..99   >
	 RES4         = BS1[56    ]
*/
typedef   struct 
{
	uint16_t         u16Milliseconds;
	unsigned char    ucMinutes :6;
	unsigned char    ucRES1    :1;
	unsigned char    ucIV      :1;
	unsigned char    ucHours   :5;
	unsigned char    RES2      :2;
	unsigned char    ucSU      :1;
	unsigned char    ucDayOfMonth :5;
	unsigned char    ucDayOfWeek  :3;
	unsigned char    ucMonths  :4;
	unsigned char    ucRES3    :4;
	unsigned char    ucYears   :7;
	unsigned char    ucRES4    :1;
} CP56Time2a;

extern  CP56Time2a MakeCP56Time2a(TimeStruct t);
extern  CP56Time2a MakeCP56Time2aFromCP56(CP56Time2a *tm);

extern  TimeStruct ParseCP56Time2a(CP56Time2a stTime);

/*
  ����/��Ӧ��(S1)
  �Ӷ�վ����ش�����վ�ı��ģ�����վҲ��֪���Ӷ�վ�Ƿ���յ����ģ���Уʱ
  ����/ȷ�ϣ�S2��
  �Ӷ�վ���պ���Ҫ������վ��ȷ�ϱ���
  ͨ�����ڷ��Ͳ��������Ϳ�������ȡ�
  ����/��Ӧ(S3)
  �Ӷ�վ��������վ�������ĺ���������Ӧ����վ���������ٻ����ݡ�����I�����ݡ�����II�����ݡ�������·״̬�ȡ�
  
  ƽ����·����ģʽ�̶�֡����������
  Bit  D7    D6    D5    D4   D3 D2 D1 D0
  ���� DIR   PRM   FCB   FCV  ��·������FC
  ���� DIR   PRM   RES   DFC  
  -------------------------------------------------------------------------------------
  ��������Ĺ�����ͷ���      ���������FCVλ״̬         �Ӷ������������Ĺ�����ͷ���
  -------------------------------------------------------------------------------------
  <0>��λԶ����·                    0                    <0>ȷ�ϣ��Ͽ�
                                                          <1>ȷ�ϣ����Ͽ�
  -------------------------------------------------------------------------------------
  <2>����/ȷ�� ��·���Թ���          0                    <0>ȷ�ϣ��Ͽ�
	                                                        <1>ȷ�ϣ����Ͽ�
	--------------------------------------------------------------------------------------
  <3>����/ȷ�� �û�����              1                    <0>ȷ�ϣ��Ͽ�
                                                          <1>ȷ�ϣ����Ͽ�
  -------------------------------------------------------------------------------------
  <4>����/�޻ش��û�����             0                    �޻ش�
  -------------------------------------------------------------------------------------
  <9>����/��Ӧ ������·״̬          0                    <11>��Ӧ����·״̬
	-------------------------------------------------------------------------------------
	��һ��˵����
	��1���Ϸ�������ĳЩʡ�ݹ涨�ն��豸������֡�п�����DIR����Ϊ1����վ�·�Ϊ0���ն��豸Ҳ����վ��
	     ����������������һ���Խ�����������µ��л���
	��2�����ն˺���վ������Ϊ������վ������£��������е���վ�����е�ACD��DFC�Ѿ�ʧȥ���塣
	��3����ƽ��ģʽ��ֻ�й�����Ϊ3��Ҳ���ǡ�����/ȷ���û����ݡ���FCV����Ч���Ż���FCB��¼�ͷ�ת����ʱ
	     ���ǳ�֡�����̶�֡�е�FCB��FCV����0����վ����վ��Ҫ��¼�Է���֡��FCB״̬����֡ÿ�ɹ���һ��
			 �ͷ�ת1�Σ��Է��յ���ͬ��FCB��˵���п������ط�֡��
	-----------------------------------------------------------------------------------------------
*/

typedef struct 
{
	/**************************************************************************
	<<ƽ����·��־λ>>
	DIR:���䷽��λ
	DIR=0:��ʾ��֡����������վ������������
	DIR=1:��ʾ��֡���������ն˷�����������	
	***************************************************************************/
	bool bDIR;
	
	/***************************************************************************
	PRM:������־λ����վ����վλ1����վ����վλ0
	PRM=1:��ʾ��֡������������վ
	PRM=0:��ʾ��֡�������ԴӶ�վ	
	****************************************************************************/
	bool bPRM;
	
	/************************************************************************************
	FCB:֡����λ����վÿ���վ������һ�ֵġ�����/ȷ�ϡ�������/��Ӧ���������ʱ����FCBȡ����
	��վΪÿ����վ����һ��FCB�Ŀ���������ʱδ�յ�Ӧ������վ�ط����ط����ĵ�FCB���ֲ��䣬�ط�
	������಻����3�Ρ����ط�3�κ���δ�յ�Ԥ��Ӧ����������ִ������
  
  ��֡������ЧλFCV=1ʱ����ʾÿ��վ�����ķ���/ȷ�ϻ�����/��Ӧ����ı仯λ��������ֹ��Ϣ����Ķ�ʧ���ظ���
  �ն˻���վFCBλ��0��ʼ��ת�����ն˺���վ�����ĵ�һ֡��Ч�ı�����FCB=0.
  ����վ��ͬһ�Ӷ�վ�����µķ���/ȷ�ϻ�����/��Ӧ�������ʱ����FCBȡ��ֵ������վ����ÿһ�Ӷ�վFCBֵ������ʱδ�յ�
  �Ӷ�վ�ı��ģ�����ճ��ֲ����������վ���ı�FCB��״̬���ظ�ԭ���ķ���/ȷ�ϻ�������/��Ӧ���񡣸�λ�����е�FCB=0���Ӷ�վ���ո�λ�����FCB�á�0����
	*****************************************************************************/
	bool bFCB;
	
	/**********************************************************************************
	FCV:֡������Чλ
	FCV=1:��ʾFCB��Ч
	FCV=0:��ʾFCB��Ч	
	
	FCV--FCB��Чλ��S2,S3����ʱΪ1��S1����ʱΪ0
	****************************************************************************/
  bool bFCV;
	/****************
	��·������
	****************/
	unsigned char ucFC;
} LinkControlContent;

extern void ParseLinkControl(unsigned ucLinkControl,LinkControlContent* LCC);
typedef enum 
{
	eFC_ResetRemoteLink          = 0, //��λԶ����·
	eFC_Confirm_OK               = 0, //ȷ��_�Ͽ�
	eFC_Confirm_NO               = 1, //ȷ��_��
	eFC_Send_Confirm_LinkTestFun = 2, //����_ȷ��_��·���Թ���
    eFC_Send_Confirm_UserData    = 3, //����_ȷ��_�û�����
    eFC_Send_NoAnswerUserData    = 4, //����_�޻ش��û�����
	eFC_Request_LinkState        = 9, //����_��·״̬
	eFC_Response_LinkState       = 11 //��Ӧ_��·״̬
} enum_FC_BALANCELINKFUNCODE;

typedef enum 
{
	//1)	���ӷ���Ĺ�����Ϣ
	eTI_M_SP_NA_1_SingleInfo                                = 1,//������Ϣ
	eTI_M_DP_NA_1_DoubleInfo                                = 3,//˫����Ϣ
	eTI_M_ME_NA_1_Measure_Normalized                        = 9,//��ֵ_��һ��ֵ
	eTI_M_ME_NB_1_Measure_Scaled                            = 11,//��ֵ_��Ȼ�ֵ 
	eTI_M_ME_NC_1_Measure_ShortFloatValue                   = 13,//��ֵ_�̸�����
	eTI_M_SP_TB_1_CP56Time2aBasedSingleInfo                 = 30,//��ʱ��ĵ�����Ϣ
	eTI_M_DP_TB_1_CP56Time2aBasedDoubleInfo                 = 31,//��ʱ���˫����Ϣ
	eTI_M_FT_NA_1_FaultEventInfo                            = 42,//�����¼���Ϣ
	eTI_M_IT_NB_1_Accumulate_ShortFloatValue                = 206,//�ۼ���_�̸�����
	eTI_M_IT_TC_1_CP56Time2aBasedAccumulate_ShortFloatValue = 207,//��ʱ����ۼ���_�̸�����
	//2)	���Ʒ���Ĺ�����Ϣ
	eTI_C_SC_NA_1_SingleCommand       = 45,//�������� 
	eTI_C_SC_NB_1_DoubleCommand       = 46,//˫������
	//3)	�ڼ��ӷ����ϵͳ����
	eTI_M_EI_NA_1_InitComplete        = 70, //��ʼ������
	//4)	�ڿ��Ʒ����ϵͳ����
	eTI_C_IC_NA_1_MainStationCallOnCommand   = 100, //��վ���ٻ�����
	eTI_C_CI_NA_1_ElectricValueCallOnCommand = 101,//�������ٻ�����
	eTI_C_CS_NA_1_ClockSynCommand            = 103,//ʱ��ͬ������ 
	eTI_C_TS_NA_1_TestCommand                = 104,//��������
	eTI_C_RP_NA_1_ResetProcessCommand        = 105,//��λ��������
	eTI_C_CD_NA_1_DelayedSampleCommand       = 106,//��ʱ�������
	eTI_C_SR_NA_1_SwitchDefiniteRange        = 200,//�л���ֵ��
	eTI_C_RR_NA_1_ReadDefiniteAreaCode       = 201,//����ֵ����
	eTI_C_RS_NA_1_ReadParaAndDefiniteValue   = 202,//�������Ͷ�ֵ
	eTI_C_WS_NA_1_WriteParaAndDefiniteValue  = 203,//д�����Ͷ�ֵ
	eTI_F_FR_NA_1_FileTransport              = 210,//�ļ�����
	eTI_F_SR_NA_1_SoftwareUpdate             = 211,//��������
	eTI_F_RE_WA_1_CallRecordData             = 111,//ʵʱ¼������
	eTI_F_RE_WA_2_CallRecordData             = 110 //��ʷ¼������
	
} enum_TI_TYPEID;

/*
VSQ �ɱ�ṹ�޶��ʵĶ���
--------------------------------------------------------------------
|   7   |   6   |   5   |   4   |   3   |   2   |   1   |  0   |
--------------------------------------------------------------------
   SQ   | Num
SQ<0>:����Ϣ�����ַѰַ�ĵ�����ϢԪ�ػ�Ԫ�ؼ���
SQ<1>:������ϢԪ�ػ���ϢԪ��ͬ�༯�ϵ�����
SQ<0>:ÿ����Ϣ��ֱ������Ϣ���ַ
SQ<1>:������Ϣ�干����Ϣ���ַ��������Ϣ���ַ���μ�1
Num:��Ϣ����Ŀ 
*/
typedef struct 
{
	unsigned char ucNumber :7;
	unsigned char ucSQ     :1;
}  VSQ_VarStructureQualify;

/*
����ԭ����ֵ���嶨�� 
����ԭ��=CAUSE OF TRANSMISSION := CP16{Cause,P/N,T,Originator Address(opt)}
����                       Cause :=UI6[1..6]<0..63>
<0>     := δ���� 
<1..63>  :=����ԭ�����
<1..47>  :=�����ױ�׼�ı�׼����(���ݷ�Χ)����14
<48..63> := ר�÷�Χ
P/N     :=  BS1[7]<0..1>
<0>	:= �϶�ȷ��
<1>	:= ��ȷ��
T=test := BS1[8]<0..1>
<0>:=δ����  
<1>:=����
Originator address=Դ���ߵ�ַ := UI8[9..16]
<0>:=ȱʡֵ   
<1..255>   :=Դ���ߵ�ַ��

P/Nλ���ڶ���ʼ��Ӧ�ù���������ļ����Կ϶����߷�ȷ��,���޹ص������P/N���㡣
*/

typedef enum 
{
	eCOT_NoUse                              = 0,  //δ��
	eCOT_per_cyc                            = 1,  //����_ѭ��
	eCOT_back_scan                          = 2,  //����ɨ��
	eCOT_spont                              = 3,  //ͻ��_�Է�                 		 
	eCOT_init                               = 4,  //��ʼ��              	 
	eCOT_req                                = 5,  //������߱�����              	 
	eCOT_act                                = 6,  //����              	 
	eCOT_actcon                             = 7,  //����ȷ��                	 
	eCOT_deact                              = 8,  //ֹͣ����                	 
	eCOT_deactcon                           = 9,  //ֹͣ����ȷ��              	 
	eCOT_actterm                            = 10, //������ֹ                    	 
	eCOT_fileTrans                          = 13, //�ļ�����           	 
	eCOT_introgen                           = 20, //��Ӧվ�ٻ�                		 
	eCOT_reqcogen                           = 37, //��Ӧ�������ٻ�          	
	eCOT_unknownTID                         = 44, //δ֪�����ͱ�ʶ
	eCOT_unknownCOT                         = 45, //δ֪�Ĵ���ԭ��
	eCOT_unknownASPUcommAddr                = 46, //δ֪��Ӧ�÷������ݵ�Ԫ������ַ
	eCOT_unknownInfoObjAddr                 = 47, //δ֪����Ϣ�����ַ
	eCOT_RomoteControlStatusErr             = 48, //ң��ִ����ѹ��״̬���� 
	eCOT_RemoteControlTimeStampErr          = 49, //ң��ִ��ʱ�������
	eCOT_RemoteControlDigitalSignAuthenErr  = 50, //ң��ִ������ǩ����֤����
	//------------¼��---------------------------------------------------------    
	eCOT_RecordData                         = 51,
	eCOT_RemoteControlConfig                = 52,
	eCOT_UnDefined
}  enumCOT;

typedef struct 
{
	unsigned char ucOriginatorAddress;
	bool bTest;
	bool bPN;
	enumCOT eCOT;
	char strErrInfo[256];
} COT_Detail;


/*--------------------------------------�ն˵�¼��Э�����2018-11-1-----------------------------------------------------------------------------------------*/
/*¼������֡���ն�---->����վ*/
typedef   struct 
{
	
	FRAME_VAR_HEAD   FVH;
	uint16_t      u16InfoObjAddr;
	CP56Time2a    cp56tFileName;  //7�ֽ�
	uint32_t      u16SampleNumber;
	uint32_t      u16REServed1;
	uint32_t      u16REServed2;
	uint32_t      u16REServed3;
	uint8_t       ucQoR;
	uint8_t       ucCheckCode;
	uint8_t       ucEndChar;
} CMD_AbnormityReport;

extern void MakePackForCMD_AbnormityReport(CMD_AbnormityReport* CMD_Abnormity, uint8_t ucLinkControl, uint16_t u16LinkAddress, TimeStruct *tm, enumCOT eCOT);
extern void MakePackForCMD_FaultReport(CMD_AbnormityReport* CMD_Abnormity, uint8_t ucLinkControl, uint16_t u16LinkAddress, CP56Time2a *tm, enumCOT eCOT);
/*¼��ͨ���ṹ��*/
typedef   struct {
	unsigned long Ua;
	unsigned long Ub;
	unsigned long Uc;
	unsigned long Ia;
	unsigned long Ib;
	unsigned long Ic;
	unsigned long In;	//����ͨ��
} Sample_Point;


/*¼����Ϣ����ṹ*/

typedef   struct
{
	Sample_Point   sample_point;
	uint16_t    u16RecordObjAddr;// u16¼�������ַ;
	
} RecordWaveContent;

typedef   struct 
{

	RecordWaveContent*  PtrRecordArray;     //ָ��¼�����ݿ��׵�ַ
	CP56Time2a          RecordStartTime;    //����¼���Ŀ�ʼʱ��
	uint16_t         u16TotalPointNumber;//����¼���ܵĲ�������
	uint16_t         u16VSQPointNumber;  //֡�е�ĸ���
	uint8_t          ucFlagEndofData;    //�Ƿ����һ֡
} RecordDataBlock;

/*Ϊ�˼����ļ����䣬������ļ������ֶ�*/
typedef   struct
{	
	FRAME_VAR_HEAD   FVH;
	uint16_t         u16InfoObjAddr;
	unsigned char    ucCtype; //������������/����
	unsigned char    ucCheckCode;
	unsigned char    ucEndChar;
}CMD_SoftwareUpdateConfirm;

extern void MakePackForCMD_SoftwareUpdateStartOrStopConfirm(CMD_SoftwareUpdateConfirm* CMD_UpdateConfirm, uint8_t ucLinkControl, uint16_t u16LinkAddress, enumCOT eCOT, bool start);//startΪ1��stop Ϊ0

//typedef   struct
//{	
//	FRAME_VAR_HEAD   FVH;
//	uint16_t         u16InfoObjAddr;
//	unsigned char    ucCtype; //������������/����
//	unsigned char    ucCheckCode;
//	unsigned char    ucEndChar;
//}CMD_SoftwareUpdateConfirm;

//extern void MakePackForCMD_SoftwareUpdateStartOrStopConfirm(CMD_SoftwareUpdateConfirm* CMD_UpdateConfirm, uint8_t ucLinkControl, uint16_t u16LinkAddress, enumCOT eCOT, bool start);//startΪ1��stop Ϊ0


//-------------------------------��������-------------------------------------
typedef   struct
{	
	FRAME_VAR_HEAD   FVH;
	uint16_t         u16InfoObjAddr;
	unsigned char    ucOPerationID;
	unsigned char    ucFileNameLen;
	CP56Time2a       cp56tFileName;  //7 Bytes
	unsigned char    ucCheckCode;
	unsigned char    ucEndChar;
}CMD_RealRecWave;

extern void MakePackForCMD_RealRecWave(CMD_RealRecWave* CMD_RealRecWav, uint8_t ucLinkControl, uint16_t u16LinkAddress, TimeStruct *tm, enumCOT eCOT);

//�ļ��������Ϣ�ṹ�壬�趨�ļ�������󳤶�Ϊһ���ֽڱ�ʾ�ķ�Χ256
typedef union 
{
	uint8_t data[266];
	  struct FILEINFO
	{
		uint8_t optID;
		uint8_t fileNameLen;
		uint8_t fileName[256];
		uint32_t fileID;
		uint32_t fileLen;		
	} Info;	
} FILEACTON_T;

/*д�ļ����ݴ���ṹ��-
       ������󳤶� ������͵�ַ�� ���ͱ�ʶ��  �ɱ�֡���޶���  ����ԭ��  ASDU������ַ
(��С��    255           -3            -1           -1            -2          -2         = 246
*/
typedef   struct
{
	uint8_t  optID;
	uint32_t fileID;
	uint32_t fileSecNo;
	uint8_t  haveMoreData;
	uint8_t  checkCode;		
} UPDATEDATA_T;



//-------------------------------��������-------------------------------------



//----------------------------------------------------------------------------------------------------------------------------------------------------------------
//���ٻ�
typedef   struct CMD_GeneralInterrogation
{
	/*
	����ʵ��:
	15:28:26 682	δ֪֡ ������	68 0C 0C 68 73 02 00 64 01 06 00 02 00 00 00 14 F6 16           //��վ�������ٻ�
	15:28:26 852	δ֪֡ ���ա�	10 80 02 00 82 16                                               //�ն˻�ȷ�϶�֡
	15:28:26 952	δ֪֡ ���ա�	68 0C 0C 68 D3 02 00 64 01 07 00 02 00 00 00 14 57 16           //�ն˼������ٻ�
	15:28:27 532	δ֪֡ ������	10 80 02 00 82 16                                               //��վ��ȷ�϶�֡
	*/

	FRAME_VAR_HEAD   FVH;
	uint16_t u16InfoObjAddr;
	unsigned char    ucQOI;
	unsigned char    ucCS;
  unsigned char    ucEndChar;
} CMD_GeneralInterrogation;
extern void MakePackForGeneralInterrogation(CMD_GeneralInterrogation* callOn, unsigned char ucLinkControl,uint16_t u16LinkAddress,enumCOT eCOT);

//ʱ��ͬ��
typedef   struct 
{
	/*
	????:
	15:28:38 822	???	???	68 12 12 68 53 02 00 67 01 06 00 02 00 00 00 6B 2D 1F 0C EC 06 10 8A 16    //????????
	15:28:39 002	???	???	10 80 02 00 82 16                                                          //???????
	15:28:39 122	???	???	68 12 12 68 D3 02 00 67 01 07 00 02 00 00 00 F6 0E 2F 11 A8 05 52 89 16    //?????????
	15:28:40 172	???	???	10 80 02 00 82 16                                                          //???????
	*/
	
	FRAME_VAR_HEAD   FVH;
	uint16_t u16InfoObjAddr;
	CP56Time2a       st;
	unsigned char    ucCS;
  unsigned char    ucEndChar;
} CMD_TimeSyn;

//CP16Time2a :=UI16[1..16<0..59999ms>
typedef uint16_t CP16Time2a;

//????????
typedef struct 
{
	/*
	????:
	15:28:31 602	???	???	68 0D 0D 68 53 02 00 6A 01 06 00 02 00 00 00 31 00 F9 16        //??????????
	15:28:31 772	???	???	10 80 02 00 82 16                                               //???????
	15:28:31 872	???	???	68 0D 0D 68 D3 02 00 6A 01 07 00 02 00 00 00 95 00 DE 16        //?????????
	15:28:32 552	???	???	10 00 02 00 02 16 						                        //???????
	15:28:33 532	???	???	68 0D 0D 68 73 02 00 6A 01 03 00 02 00 00 00 AE 02 95 16        //??????
	15:28:33 692	???	???	10 80 02 00 82 16                                               //???????
	*/
	
	FRAME_VAR_HEAD   FVH;
	uint16_t      u16InfoObjAddr;
	CP16Time2a       st;
	unsigned char    ucCS;
    unsigned char    ucEndChar;
} CMD_DelayedSample;
extern void MakePackForDelayedSample(CMD_DelayedSample* delayedSample, unsigned char ucLinkControl,uint16_t u16LinkAddress,enumCOT eCOT,CP16Time2a __st);

//��λ��������
typedef struct 
{
	/*
	????:
	*/
	
	FRAME_VAR_HEAD   FVH;
	uint16_t  u16InfoObjAddr;
	unsigned char    ucResetProcessQRP;
	unsigned char    ucCS;
  unsigned char    ucEndChar;
} CMD_ResetProcess;

extern void MakePackForResetProcess(CMD_ResetProcess* cmdResetProcess, unsigned char ucLinkControl,uint16_t u16LinkAddress,enumCOT eCOT);

//������
//2017-10-08 ��������������1����һ��

typedef enum 
{
	eCOI_LocalPowerOn = 0,
	eCOI_LocalManualReset = 1,
	eCOI_RemoteReset    = 2
} enumCOI_InitializeCause;

//��ʼ�����������
typedef union{
   struct 
{
	/*
	����ʵ��:
	15:28:18 830	δ֪֡ ���ա�	68 0C 0C 68 F3 02 00 46 01 04 00 02 00 00 00 00 42 16           //��ʼ������
	15:28:20 372	δ֪֡ ������	10 00 02 00 02 16                                               //��վȷ�ϳ�ʼ������
	 */
	FRAME_VAR_HEAD   FVH;
	uint16_t u16InfoObjAddr;
	unsigned char    ucCOI;
	unsigned char    ucCS;
  unsigned char    ucEndChar;
} frame;
unsigned char data[18];
}CMD_InitializedOver_Type;

extern void MakePackForInitializedOver(CMD_InitializedOver_Type *cmdInitOver, unsigned char ucLinkControl,uint16_t u16LinkAddress,enumCOI_InitializeCause eCOI);

//���������
typedef struct 
{
	/*
	????:
	*/
	
	FRAME_VAR_HEAD   FVH;
	uint16_t u16InfoObjAddr;
	uint16_t u16FBP;  //�̶�����ͼ��FBP
	unsigned char    ucCS;
  unsigned char    ucEndChar;
} CMD_TestCommand;

extern void MakePackForTestCommand(CMD_TestCommand *cmdTest, unsigned char ucLinkControl,uint16_t u16LinkAddress,enumCOT eCOT);

//һ֡����
typedef   struct
{
	unsigned char ucData[264]; //255+6=261 ȡ4�ֽڶ���
	int nSize;
	
} DataPack_Type;
//��Ҫ���͵�����֡������
typedef struct SendData
{
	DataPack_Type * data;
	struct SendData *next;
	int nTotalCount;
	int nSendedIndex;	
} SendData_Type;



//############################################################################################################
/*
Ʒ�������� Quality Descriptor
OV = ���/δ���
���Ա�ʾ��Ϣ����������Ƿ񳬳���Ԥ�ȶ������ֵ��Χ����Ҫ����ģ�������ݣ�

BL = ������/δ������
��Ϣ�����ֵ���������䣬�����ַ���ǰ�ɼ���ֵ�������ͽ��������ɵ��������������Զ�װ������

SB = ���滻/δ���滻
��Ϣ�����ֵ��ֵ��Ա������Ա����������ɵ��������Զ�װ�����ṩ�����淶�涨ͨ���˻�ά���ڶԲɼ�װ�õ��˹�����Ҳ��Ϊ�滻���ݡ�

NT = ��ǰֵ/�ǵ�ǰֵ
������ɹ�ˢ�µ�ֵ�ͳ�Ϊ��ǰֵ�����ڹ涨��ʱ������ˢ�²��ɹ���Ϊ��Чʱ��Ϊ�ǵ�ǰֵ��

IV = ��Ч/��Ч
��ֵ����ȷ�ɼ�������Ч���ڲɼ�����ȷ����ϢԴ�Ĳ�����״̬��װ��ʧЧ��װ���ڷǹ���ˢ��״̬��ʱ��ֵ
Ӧ����Ϊ��Ч����Щ��������Ϣ�����ֵδ�����壬������Ч��������ʹ���ߣ���ֵ����ȷ���Ҳ���ʹ�á�

*/
//��վ�ϴ�ң������Ľ������
typedef union
{
	  struct
	{
		uint8_t ATT7022OnOrOff :1; //ATT7022�Ƿ���
		uint8_t CircuitBreakState:1;//��·��״̬
		uint8_t __Reserve:6; //������Ĭ��Ϊ0 
	} b;
	uint8_t w;
}DeviceState_Type;

extern DeviceState_Type deviceState;


//��װң���ܹ��õ�����������
//��Ʒ�������ʵĵ���ң����Ϣ
typedef union
{
  struct
  {
    uint8_t SPI:1;                      /*!< bit:  0     ����״̬��0 ��/����1��/��              */
    uint8_t _reserved0:3;               /*!< bit:  1..3  Reserved                              */
    uint8_t BL:1;                       /*!< bit:  4     ������/δ��������0 δ������;1 ������    */
    uint8_t SB:1;        		            /*!< bit:  5     ���滻/δ���滻��0 δ���滻;1 ���滻    */
	  uint8_t NT:1;                       /*!< bit:  6     ��ǰֵ/�ǵ�ǰֵ��0 ��ǰֵ;1 �ǵ�ǰֵ    */
	  uint8_t IV:1;                       /*!< bit:  7     ��Ч/��Ч��0 ��Ч;1 ��Ч    */
	} b;                                   /*!< Structure used for bit  access                  */
  uint8_t w;                            /*!< Type      used for byte access                  */
} RemoteSignalSQI_Type;
//��Ʒ�������ʵ�˫��ң����Ϣ
typedef union
{
  struct
  {
    uint8_t DPI:2;                      /*!< bit:  0..1  ����״̬��0 ��ȷ�����м�״̬��1 ��/����2 ��/�� ��3 ��ȷ��  */
    uint8_t _reserved0:2;               /*!< bit:  2..3  Reserved                              */
    uint8_t BL:1;                       /*!< bit:  4     ������/δ��������0 δ������;1 ������    */
    uint8_t SB:1;        		            /*!< bit:  5     ���滻/δ���滻��0 δ���滻;1 ���滻    */
	  uint8_t NT:1;                       /*!< bit:  6     ��ǰֵ/�ǵ�ǰֵ��0 ��ǰֵ;1 �ǵ�ǰֵ    */
	  uint8_t IV:1;                       /*!< bit:  7     ��Ч/��Ч��0 ��Ч;1 ��Ч    */
	} b;                                   /*!< Structure used for bit  access                  */
  uint8_t w;                            /*!< Type      used for byte access                  */
} RemoteSignalDIQ_Type;

typedef struct 
{
	uint16_t u16RemoteSignalObjAddr;
	unsigned char    ucSignal;
	bool             bHaveCP56Time2a;
	CP56Time2a       st;
}  SignalContent_Type;

typedef struct SignalConLink
{
	SignalContent_Type * signalCon;
	struct SignalConLink *next;
	
} SignalContentLink_Type;



//############################################################################################################
//��վ�ϴ�ң������Ľ������
//��Ʒ�������ʵĵ���ң����Ϣ
typedef union
{
  struct
  {
    uint8_t OV:1;                      /*!< bit:  0      �Ƿ������0 û�������1 �������              */
    uint8_t _reserved0:3;               /*!< bit:  1..3  Reserved                              */
    uint8_t BL:1;                       /*!< bit:  4     ������/δ��������0 δ������;1 ������    */
    uint8_t SB:1;        		            /*!< bit:  5     ���滻/δ���滻��0 δ���滻;1 ���滻    */
	  uint8_t NT:1;                       /*!< bit:  6     ��ǰֵ/�ǵ�ǰֵ��0 ��ǰֵ;1 �ǵ�ǰֵ    */
	  uint8_t IV:1;                       /*!< bit:  7     ��Ч/��Ч��0 ��Ч;1 ��Ч    */
	} b;                                   /*!< Structure used for bit  access                  */
  uint8_t w;                            /*!< Type      used for byte access                  */
} RemoteMeasureQDS_Type;


typedef struct 
{
	uint16_t u16RemoteMeasureObjAddr;
	float            fMeasure;
	unsigned char    ucQDS;
} MeasureContent;

typedef struct MeasureLink
{
	MeasureContent* measureCon;
	struct MeasureLink *next;
} MeasureContentLink;





/**********************ң�����������*************************/
#define NUMYX_MAX          2
#define ADDRESS_YX_BEAS    0x5001
extern SendData_Type   g_pRemoteSignalSendAll;
extern DataPack_Type   g_RemoteSignalFrame;

/*¼��֡��¼��֡����*/
extern SendData_Type   g_pRecordWaveFrameList;
extern DataPack_Type   g_RecordWaveFrame;
extern uint8_t      g_nRemoteSignalSendedIndex;
extern void            MakePackForRemoteSignal(DataPack_Type *m_pSendFrame, DeviceState_Type m_dState,uint16_t m_ASDUAdress);
/**********************ң�����������*************************/
#define NUMYC_MAX          40
#define ADDRESSYC_BEAS     0x5001

typedef union{	
    struct {
    unsigned long e_Pa;//01H,A���й�����
    unsigned long e_Pb;//02H,B���й�����
    unsigned long e_Pc;// 03H,C���й�����
    unsigned long e_Qa;// 05H, A���޹�����  
    unsigned long e_Qb;// 06H, B���޹�����
    unsigned long e_Qc;// 07H,C���޹�����
    unsigned long e_Sa;// 09H, A�����ڹ���
    unsigned long e_Sb;// 0AH, B�����ڹ���
    unsigned long e_Sc;// 0BH, C�����ڹ���
    unsigned long e_Ua;// 0DH, A���ѹ��Чֵ
    unsigned long e_Ub;// 0EH, B���ѹ��Чֵ
    unsigned long e_Uc;// 0FH, C���ѹ��Чֵ
    unsigned long e_Ia;// 10H, A�������Чֵ
    unsigned long e_Ib;// 11H, B�������Чֵ
    unsigned long e_Ic;// 12H, C�������Чֵ
    unsigned long e_It;// 13H, �������ʸ���͵���Чֵ
    unsigned long e_Pfa;//14H,A�๦������ 
    unsigned long e_Pfb;// 15H,B�๦������
    unsigned long e_Pfc;// 16H,C�๦������
    unsigned long e_Pga;//18H,A��������ѹ���
    unsigned long e_Pgb;// 19H,B��������ѹ���
    unsigned long e_Pgc;// 1AH,C��������ѹ���
    unsigned long e_Freq;//1CH,��Ƶ��
    unsigned long e_Epa;//1EH,A���й�����
    unsigned long e_Epb;// 1FH,B���й�����
    unsigned long e_Epc;// 20H,C���й�����
    unsigned long e_Eqa;//22H,A���޹�����
    unsigned long e_Eqb;// 23H,B���޹�����
    unsigned long e_Eqc;// 24H,C���޹�����
    unsigned long e_Yuaub;//26H,Ua��Ub�ĵ�ѹ�н�
    unsigned long e_Yuauc;// 27H,Ua��Uc�ĵ�ѹ�н�
    unsigned long e_Yubuc;// 28H,Ub��Uc�ĵ�ѹ�н�
    unsigned long e_RmsADC7;//29H,����·ADC�����ź���Чֵ
    unsigned long e_TPSD;//2AH,�¶ȴ����������
    unsigned long e_UtRms;//2B,�����ѹʸ���͵���Чֵ
    unsigned long e_Sflag;//2C,��Ŷ��ࡢ����SIG�ȱ�־״̬
    unsigned long PSW;//����״̬�� 4B
    unsigned long switchNumb;//������ 4B
		unsigned long _reserve1;
		unsigned long _reserve2;
} b;
unsigned long data[40];
	
} RemoteMeasureData_Type;//��ʱ����������56B (��38����Ա��38x4=152�ֽ�)

extern SendData_Type   g_pRemoteMeasureAll;
extern DataPack_Type   g_RemoteMeasureFrame;
extern uint16_t     m_nRECWaveAddr;
extern TimeStruct      timeSpanHeartBeat;

extern RemoteMeasureData_Type remoteMeasure;
extern void MakePackForRemoteMeasure(DataPack_Type *m_pSendFrame,RemoteMeasureData_Type *m_pRemoteMeasure, uint16_t m_ASDUAdress);
/*********************���������������************************/




extern unsigned char MakeLinkControl(bool bDIR,bool bPRM,bool bFCB,bool bFCV,unsigned char ucCode);
extern bool IsFrameFix(unsigned char* pBuffer,int nBufferSize);
extern void ParseCOT(uint16_t u16COT,COT_Detail* CTD);
extern bool ParseMeasure(unsigned char* pBuffer,int nBufferSize,MeasureContentLink* Array);
extern void MakePackForTimeSyn(CMD_TimeSyn* timeSyn, unsigned char ucLinkControl,uint16_t  u16LinkAddress,enumCOT eCOT,CP56Time2a __st);

//-----------------------------------------------------¼��-----------------------------------------------------------------------------------------
//extern SendData_Type      g_pRecordWaveFrameList;
//extern DataPack_Type      g_RecordWaveFrameFrame;
extern void MakePackForRecordData(DataPack_Type *pSendFrame,bool isLastDataFrame, uint16_t u16LinkAddress, uint8_t DataSizeInPack);
/*ʵʱ¼�� for Test*/
extern void MakePackForRecordData_(DataPack_Type *pSendFrame,bool isLastDataFrame, uint16_t u16LinkAddress, uint8_t DataSizeInPack);
/*ʵʱ¼��V2*/
extern void MakePackForRealtimeRecordData(DataPack_Type *pSendFrame,bool isLastDataFrame, uint16_t u16LinkAddress, uint8_t DataSizeInPack);

extern void MakePackRecordDataForFault(DataPack_Type *pSendFrame,bool isLastDataFrame, uint16_t u16LinkAddress, uint16_t curPos, uint8_t byteOfDataSizeInPack);
uint32_t GetDifferenceTime(TimeStruct * curr, TimeStruct *old);
void  RemoteMeasureUpdate(void);
#endif

